aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/invoice/[id]/+page.svelte
blob: 84b035939de5d9b50b7cccd4b907d06a83a99541 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<script>
	import { copy, f, get, reverseFormat, s, sats } from '$lib/utils';
	import { browser } from '$app/environment';
	import { invoices } from '$lib/store';
	import { Icon, Heart, Image, Qr, Tip } from '$comp';
	import { goto } from '$app/navigation';
	import { t } from '$lib/translations';

	export let data;
	$: refresh(data);
	let { invoice, id, user } = data;
	let {
		amount,
		rate,
		received,
		prompt,
		text,
		tip,
		user: { username, currency }
	} = invoice;

	let showMobileTip = false;

	let qr;
	let tipPercent = 0;

	let fullscreen;

	let refresh = (data) => {
		({ invoice, id } = data);
		({
			amount,
			rate,
			received,
			prompt,
			text,
			tip,
			user: { username, currency }
		} = invoice);

		tipPercent = (tip / amount) * 100;
	};

	$: tip = Math.round((amount / 100) * tipPercent);

	$invoices[id] = { amount, id, rate, received, text, tip, username };
	$: browser &&
		$invoices[id]?.received >= $invoices[id]?.amount &&
		goto(($invoices[id].memo === 'launch' && '/launch/purchase') || `/invoice/${id}/paid`);

	$: amountFiat = parseFloat(((amount * rate) / sats).toFixed(2));

	$: tipAmount = ((tip * rate) / sats).toFixed(2);

	$: invoiceAmountFiatFormatted = f(amountFiat, currency);
</script>

<div class:full-shadow={showMobileTip}>
	<button
		class="ml-5 md:ml-20 mt-5 md:mt-10 hover:opacity-80"
		class:invisible={!user}
		disabled={showMobileTip}
		on:click={() => goto(`/${username}/receive`)}
	>
		<Icon icon="arrow-left" style="w-10" />
	</button>

	<div class="flex justify-center items-center mb-20 md:mt-[20px] px-3">
		<div
			class="block lg:flex justify-center items-center space-y-10 lg:space-y-0 space-x-0 lg:space-x-24"
		>
			{#if invoice.prompt}
				<Tip
					bind:amountFiat
					bind:showMobileTip
					bind:tipPercent
					bind:tipAmount
					bind:amount
					bind:tip
					bind:currency
					bind:username
				/>
			{/if}

			<!-- invoice section -->
			<div class="text-center space-y-5">
				<button
					class="block md:hidden bg-black {showMobileTip
						? 'text-white/50'
						: 'text-white'} rounded-xl px-6 py-3 block text-sm font-semibold mx-auto flex justify-center items-center"
					on:click={() => (showMobileTip = true)}
					disabled={showMobileTip}
				>
					<Heart />
					{$t('invoice.addTip')}
				</button>

				<!-- found missing translation --->
				<span class="text-secondary block text-2xl break-all"
					>Scan to pay <a href="/{username}" class="text-black font-semibold hover:opacity-80"
						>{username}</a
					></span
				>

				<Qr {text} image={'/images/invoice.svg'} disabled={showMobileTip} bind:qr />

				<div>
					<button
						class="flex rounded-full border py-2 px-5 font-bold hover:opacity-80 mb-2 mx-auto"
						on:click={() => copy(text)}
						><Icon icon="copy" style="mr-1" />
						<div>Copy</div></button
					>
				</div>

				<div class="px-5 space-y-3">
					<div class="flex justify-between">
						<span class="font-semibold text-sm">{$t('invoice.invoice')}</span>
						<span class="font-semibold text-sm"
							>{invoiceAmountFiatFormatted}
							<span class="text-secondary font-normal">{`(${s(amount)} SAT)`}</span></span
						>
					</div>

					<div class="flex justify-between">
						<span class="font-semibold text-sm">{$t('invoice.tip')}</span>
						<span class="font-semibold text-sm"
							>{f(tipAmount, currency)}
							<span class="text-secondary font-normal">{`(${s(tip)} SAT)`}</span></span
						>
					</div>

					<div class="flex flex-wrap justify-between">
						<span class="font-bold mr-1">{$t('invoice.total')}</span>
						<span class="font-bold"
							>{f(amountFiat + parseFloat(tipAmount), currency)}
							<span class="text-secondary font-normal">{`(${s(tip + amount)} SAT)`}</span></span
						>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<style>
	.full-shadow {
		box-shadow: inset 0 0 0 1000px rgba(0, 0, 0, 0.25);
	}
</style>